package com.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.util.R;
import com.util.ScreenUtil;
import com.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by Administrator on 2016/5/24.
 */
public class DropDownMenuActivity extends BaseActivity {
    @Bind(R.id.lv_content_dropDownMenu)
    ListView lv;
    @Bind(R.id.fl_dropDownMenu)
    FrameLayout fl_dropDownMenu;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.tv3)
    TextView tv3;
    @Bind(R.id.tv4)
    TextView tv4;
    private boolean isFirstDropDown = true;
    private boolean isDropDown = false;
    private List<String> cityList;
    private List<String> ageList;
    private List<String> sexList;
    private List<String> constellationList;
    private List<String> displayMenuContentList = null;
    private int currentDisplayMenuPos = 1;
    private ObjectAnimator menuDropAnimator = null;
    private ObjectAnimator menuPullAnimator = null;
    private int animStartY;
    private int animEndY;
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acty_drop_down_menu);
        ButterKnife.bind(this);
        initData();
    }

    @OnClick(R.id.bt_other)
    public void onOtherButtonClick(){
        ToastUtil.showBottomToast("内容区域");
    }@OnClick(R.id.fl_dropDownMenu)
    public void onDropDownMenuClick(){
        ToastUtil.showBottomToast("菜单布局空白区域");
        restoreTitle();
        startMenuDropOrPullAnim(1);
        isDropDown = false;
    }
    @OnClick({R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4})
    public void onTitleClick(View v) {

        switch (v.getId()) {
            case R.id.tv1:
                if (isFirstDropDown) {
                    initMenuContent(1);
                    currentDisplayMenuPos = 1;
                    startFirstDropAnim();
                    isFirstDropDown = false;
                    isDropDown = true;
                } else {
                    if (isDropDown) {
                        if (currentDisplayMenuPos == 1) {
                            startMenuDropOrPullAnim(1);
                            restoreTitle();
                            isDropDown = false;
                        } else {
                            restoreTitle();
                            tv1.setText(tv1.getText().toString().replace("∨", "∧"));
                            initMenuContent(1);
                            currentDisplayMenuPos = 1;
                        }
                    } else {
                        initMenuContent(1);
                        currentDisplayMenuPos = 1;
                        startMenuDropOrPullAnim(0);
                        setTitleText();
                        isDropDown = true;
                    }
                }
                break;
            case R.id.tv2:
                if (isFirstDropDown) {
                    initMenuContent(2);
                    currentDisplayMenuPos = 2;
                    startFirstDropAnim();
                    isFirstDropDown = false;
                    isDropDown = true;
                } else {
                    if (isDropDown) {
                        if (currentDisplayMenuPos == 2) {
                            startMenuDropOrPullAnim(1);
                            restoreTitle();
                            isDropDown = false;
                        } else {
                            restoreTitle();
                            tv2.setText(tv2.getText().toString().replace("∨", "∧"));
                            initMenuContent(2);
                            currentDisplayMenuPos = 2;
                        }
                    } else {
                        initMenuContent(2);
                        currentDisplayMenuPos = 2;
                        startMenuDropOrPullAnim(0);
                        setTitleText();
                        isDropDown = true;
                    }
                }
                break;
            case R.id.tv3:
                if (isFirstDropDown) {
                    initMenuContent(3);
                    currentDisplayMenuPos = 3;
                    startFirstDropAnim();
                    isFirstDropDown = false;
                    isDropDown = true;
                } else {
                    if (isDropDown) {
                        if (currentDisplayMenuPos == 3) {
                            startMenuDropOrPullAnim(1);
                            restoreTitle();
                            isDropDown = false;
                        } else {
                            restoreTitle();
                            tv3.setText(tv3.getText().toString().replace("∨", "∧"));
                            initMenuContent(3);
                            currentDisplayMenuPos = 3;
                        }
                    } else {
                        initMenuContent(3);
                        currentDisplayMenuPos = 3;
                        startMenuDropOrPullAnim(0);
                        setTitleText();
                        isDropDown = true;
                    }
                }
                break;
            case R.id.tv4:
                if (isFirstDropDown) {
                    initMenuContent(4);
                    currentDisplayMenuPos = 4;
                    startFirstDropAnim();
                    isFirstDropDown = false;
                    isDropDown = true;
                } else {
                    if (isDropDown) {
                        if (currentDisplayMenuPos == 4) {
                            startMenuDropOrPullAnim(1);
                            restoreTitle();
                            isDropDown = false;
                        } else {
                            restoreTitle();
                            tv4.setText(tv4.getText().toString().replace("∨", "∧"));
                            initMenuContent(4);
                            currentDisplayMenuPos = 4;
                        }
                    } else {
                        initMenuContent(4);
                        currentDisplayMenuPos = 4;
                        startMenuDropOrPullAnim(0);
                        setTitleText();
                        isDropDown = true;
                    }
                }
                break;
        }
    }

    @OnItemClick(R.id.lv_content_dropDownMenu)
    public void onDropDownMenuItemClick(int pos) {
        ToastUtil.showBottomToast(displayMenuContentList.get(pos));
        restoreTitle();
        startMenuDropOrPullAnim(1);
        isDropDown = false;
    }

    @Override
    protected void initData() {
        super.initData();
        animStartY =0;
        animEndY = -(ScreenUtil.getScreenHeight());
        initChildMenuContentList();
    }

    private void initChildMenuContentList() {
        cityList = new ArrayList<>();
        cityList.add("不限");
        cityList.add("北京");
        cityList.add("天津");
        cityList.add("上海");
        cityList.add("重庆");
        cityList.add("四川");
        cityList.add("广东");
        cityList.add("浙江");
        cityList.add("福建");
        cityList.add("广西");

        ageList = new ArrayList<>();
        ageList.add("不限");
        ageList.add("18岁以下");
        ageList.add("18-22岁");
        ageList.add("23-26岁");
        ageList.add("27-35岁");
        ageList.add("35岁以上");
        sexList = new ArrayList<>();
        sexList.add("不限");
        sexList.add("男");
        sexList.add("女");

        constellationList = new ArrayList<>();
        constellationList.add("不限");
        constellationList.add("白羊座");
        constellationList.add("金牛座");
        constellationList.add("双子座");
        constellationList.add("巨蟹座");
        constellationList.add("狮子座");
        constellationList.add("处女座");
        constellationList.add("天秤座");
        constellationList.add("天蝎座");
        constellationList.add("射手座");
        constellationList.add("摩羯座");
        constellationList.add("水瓶座");
        constellationList.add("双鱼座");
    }

    private void startMenuDropOrPullAnim(int animType) {
        if (animType == 0) {
            if (menuDropAnimator == null) {
                menuDropAnimator = new ObjectAnimator().ofFloat(fl_dropDownMenu, "translationY", animEndY, animStartY).setDuration(500);
                menuDropAnimator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        setTitleClick(false);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        setTitleClick(true);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });
            }
            menuDropAnimator.start();
            return;
        }
        if (animType == 1) {
            if (menuPullAnimator == null) {
                menuPullAnimator = new ObjectAnimator().
                        ofFloat(fl_dropDownMenu, "translationY", animStartY, animEndY)
                        .setDuration(500);
                menuPullAnimator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        setTitleClick(false);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        setTitleClick(true);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });
            }
            menuPullAnimator.start();
            return;
        }

    }

    private void restoreTitle() {
        switch (currentDisplayMenuPos) {
            case 1:
                tv1.setText(tv1.getText().toString().replace("∧", "∨"));
                break;
            case 2:
                tv2.setText(tv2.getText().toString().replace("∧", "∨"));
                break;
            case 3:
                tv3.setText(tv3.getText().toString().replace("∧", "∨"));
                break;
            case 4:
                tv4.setText(tv4.getText().toString().replace("∧", "∨"));
                break;
        }
    }

    private void setTitleText() {
        switch (currentDisplayMenuPos) {
            case 1:
                tv1.setText(tv1.getText().toString().replace("∨", "∧"));
                break;
            case 2:
                tv2.setText(tv2.getText().toString().replace("∨", "∧"));
                break;
            case 3:
                tv3.setText(tv3.getText().toString().replace("∨", "∧"));
                break;
            case 4:
                tv4.setText(tv4.getText().toString().replace("∨", "∧"));
                break;
        }
    }

    private void startFirstDropAnim() {
        final ObjectAnimator firstDropAnimator = new ObjectAnimator().
                ofFloat(fl_dropDownMenu, "translationY", animEndY, animStartY).setDuration(500);
        firstDropAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                setTitleText();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                setTitleClick(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        ObjectAnimator firstPullAnimator = new ObjectAnimator()
                .ofFloat(fl_dropDownMenu, "translationY", animStartY, animEndY).setDuration(1);
        firstPullAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                setTitleClick(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                fl_dropDownMenu.setVisibility(View.VISIBLE);
                firstDropAnimator.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        firstPullAnimator.start();
    }

    private void setTitleClick(boolean isClick) {
        if (isClick) {
            tv1.setClickable(true);
            tv2.setClickable(true);
            tv3.setClickable(true);
            tv4.setClickable(true);
//            lv.setEnabled(true);
//            fl_dropDownMenu.setClickable(true);
        } else {
            tv1.setClickable(false);
            tv2.setClickable(false);
            tv3.setClickable(false);
            tv4.setClickable(false);
//            lv.setEnabled(false);
//            fl_dropDownMenu.setClickable(false);
        }
    }

    private void initMenuContent(int which) {
        if (displayMenuContentList == null) {
            displayMenuContentList = new ArrayList<>();
            adapter = new ArrayAdapter(DropDownMenuActivity.this, android.R.layout.simple_dropdown_item_1line, displayMenuContentList);
            lv.setAdapter(adapter);
        } else {
            displayMenuContentList.clear();
        }
        switch (which) {
            case 1:
                displayMenuContentList.addAll(cityList);
                break;
            case 2:
                displayMenuContentList.addAll(ageList);
                break;
            case 3:
                displayMenuContentList.addAll(sexList);
                break;
            case 4:
                displayMenuContentList.addAll(constellationList);
                break;
        }
        adapter.notifyDataSetChanged();
    }
}
